package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.util.DesktopUtil;
	
	import flash.display.NativeWindow;
	import flash.events.Event;
	
	import mx.core.UIComponent;
	
	public class RogoBoxMediator extends Mediator
	{
		public function RogoBoxMediator(viewComponent:UIComponent)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'MicroblogMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Methdos
		//
		//--------------------------------------------------------------------------
		
		public function get view():RogoBox
		{
			return viewComponent as RogoBox;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methdos
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.LOGIN_RESULT,
				AppFacade.LOGIN_FAIL,
				AppFacade.LOADING_START,
				AppFacade.LOADING_END
			];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case AppFacade.LOGIN_RESULT:
					view.currentState = 'login';
					break;
				case AppFacade.LOADING_START:
					view.loadingBar.visible = true;
					break;
				case AppFacade.LOADING_END:
					view.loadingBar.visible = false;
					break;
			}
		}
		
		override public function onRegister():void
		{
			DesktopUtil.moveToCenter(view.nativeWindow);
			
			view.addEventListener(Event.CLOSING, view_closingHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function view_closingHandler(e:Event):void
		{
			for each(var window:NativeWindow in view.nativeApplication.openedWindows)
			{
				window.close();
			}
		}
		
	}
}